package com.feige.result;

import com.feige.enums.ResultEnumCode;

/**
 * @author feige<br />
 * @ClassName: R <br/>
 * @Description: <br/>
 * @date: 2021/9/2 9:29<br/>
 */
public class R<T> {

    private Integer code;
    private String msg;
    private T data;

    public Integer getCode() {
        return code;
    }

    public R<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public R<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }

    public static <T> R<T> getInstance(Integer code,String msg,T data){
        return new R<T>()
                .setCode(code)
                .setMsg(msg)
                .setData(data);
    }

    public static <T> R<T> getInstance(ResultEnumCode resultEnumCode, T data){
        return  new R<T>()
                .setCode(resultEnumCode.getCode())
                .setMsg(resultEnumCode.getMsg())
                .setData(data);
    }

    public static <T> R<T> ok(T data){
        return getInstance(ResultEnumCode.success,data);
    }

    public static <T> R<T> isOk(boolean flag){
        if (flag){
            return ok();
        }
        return error();
    }

    public static <T> R<T> ok(){
        return getInstance(ResultEnumCode.success,null);
    }

    public static <T> R<T> error(){
        return getInstance(ResultEnumCode.error,null);
    }

    public static <T> R<T> error(T data){
        return getInstance(ResultEnumCode.error,data);
    }


    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
